<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 去掉头尾 */
      var rob = function (nums) {
        let nums1 = nums.slice(1)
        let nums2 = nums.slice(0, nums.length - 1)
        function myRob(nums) {
          let dp = new Array(nums.length + 1).fill(0)
          dp[1] = nums[0]
          dp[2] = Math.max(nums[0], nums[1])
          for (let i = 3; i < dp.length; i++) {
            dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1])
          }
          return dp[dp.length - 1]
        }

        return Math.max(myRob(nums1), myRob(nums2))
      }
    </script>
  </body>
</html>
